package cn.edu.ctbu.studentmanage.api;

import cn.edu.ctbu.studentmanage.core.PageUtils;
import cn.edu.ctbu.studentmanage.core.RUtil;
import cn.edu.ctbu.studentmanage.domain.Dept;
import cn.edu.ctbu.studentmanage.service.DeptService;
import cn.edu.ctbu.studentmanage.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.validation.Valid;

@RestController
@RequestMapping("/api")
@Slf4j
public class DeptApiController {

    @Autowired
    private DeptService deptService;


    /**
     * 读取院系信息./api/deptindex
     * @return
     */
    @GetMapping("/deptindex")
    public R findAll(){
        return RUtil.success(deptService.findAll());
    }


    /**
     * 根据id查找院系./api/deptget/did
     * @param did
     * @return
     */
    @GetMapping("/deptget/{did}")
    public R get(@PathVariable Integer did) {
        Dept dept = deptService.getByDid(did);

        return RUtil.success(dept);
    }

    /**
     * 新增院系./api/deptinsert
     * @param dept
     * @param bindingResult
     * @return
     */
    @PostMapping("/deptinsert")
    public R insert(@Valid Dept dept, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            String errStr = bindingResult.getFieldError().getDefaultMessage();
            log.error(errStr);
        }
        deptService.Insert(dept);

        log.info(dept.toString());
        return RUtil.success(null);
    }

    /**
     * 更新院系信息./api/deptupdate
     * @param dept
     * @return
     */
    @PutMapping ("/deptupdate")
    public R update(Dept dept){

        deptService.update(dept);

        log.info(dept.toString());
        return RUtil.success(null);
    }

    /**
     * 根据id删除院系./api/deptdelete/did
     * @param did
     * @return
     */
    @DeleteMapping ("/deptdelete/{did}")
    public R delete(@PathVariable Integer did){

        deptService.delete(did);

        return RUtil.success(null);
//        log.info(Course.toString());
    }

    /**
     *
     * 分页读取全部内容./api/getbypagedept
     * @return
     */
    @GetMapping("/getbypagedept")
    public R findByPage(@RequestParam(value = "page",defaultValue = "0")Integer page,
                                @RequestParam(value = "size",defaultValue = "10")Integer size,
                                @RequestParam(value = "dname",defaultValue = "")String dname
    ){

        Page<Dept> deptPage ;
        if(StringUtils.isEmpty(dname)
        ){
            Pageable pageable = PageRequest.of(page,size, Sort.by("did"));
            deptPage =deptService.findAll(pageable);
        }else {
            Dept dept = new Dept();
            dept.setDname(dname);
            Pageable pageable = PageRequest.of(page,size,Sort.by("did"));
            ExampleMatcher matcher =ExampleMatcher.matching()
                    .withMatcher("dname",ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Dept> example = Example.of(dept,matcher);
//        Example<Course> example = Example.of(Course);
            deptPage = deptService.findAll(example,pageable);
        }

        PageUtils pageUtils = new PageUtils(deptPage.getContent(),deptPage.getTotalElements());
        return RUtil.success(pageUtils);
    }
}